#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <errno.h> //包含错误码声明，用于处理错误情况。

#define MAX_LEN 256 //定义了最大长度为256
#define DAVFS_TYPE "davfs" //定义了davfs的类型


//定义了一个结构体WebDAVConnection，用于存储挂载点和WebDAV服务器的URL
typedef struct {
  char mount_point[MAX_LEN];
  char webdav_url[MAX_LEN];
} WebDAVConnection;

//声明了一个检查davfs2是否安装的函数
int is_davfs2_installed();
int parse_arguments(int argc, char* argv[], WebDAVConnection* connection);
//声明了执行挂载命令的函数
int execute_mount_command(WebDAVConnection* connection, const char* action);
//声明了构建WebDAV URL的函数
void construct_webdav_url(const char* input, char* output, size_t max_len);
void print_usage(const char* program_name);
//声明了更新/etc/fstab文件的函数
int update_fstab(WebDAVConnection* connection);

//检查命令行参数数量，如果参数不足，打印使用方法并返回错误码
int main(int argc, char* argv[]) {
  if (argc < 3) {
    print_usage(argv[0]);
    return 1;
  }

//检查是否安装了davfs2，如果未安装，提示用户并返回错误码
  if (!is_davfs2_installed()) {
    fprintf(stderr, "Error: davfs2 is not installed. Please install it first.\n");
    return 1;
  }

//创建WebDAVConnection结构体实例，并解析命令行参数
  WebDAVConnection connection;

  //根据命令行参数执行相应的挂载或卸载操作
  if (parse_arguments(argc, argv, &connection) != 0) {
    return 1;
  }

  if (execute_mount_command(&connection, argv[1]) != 0) {
    return 1;
  }

//如果是挂载操作，尝试更新/etc/fstab文件
  if (strcmp(argv[1], "mount") == 0 && update_fstab(&connection) != 0) {
    return 1;
  }

  return 0;
}

int is_davfs2_installed() {
  return system("which mount.davfs > /dev/null") == 0;
}

int parse_arguments(int argc, char* argv[], WebDAVConnection* connection) {
  strncpy(connection->mount_point, argv[2], MAX_LEN);

  if (strcmp(argv[1], "mount") == 0 && argc >= 4) {
    construct_webdav_url(argv[3], connection->webdav_url, MAX_LEN);
  } else if (strcmp(argv[1], "mount") == 0) {
    fprintf(stderr, "Error: Mount operation requires a WebDAV URL.\n");
    return 1;
  }

  return 0;
}

void construct_webdav_url(const char* input, char* output, size_t max_len) {
  char protocol[] = "http://";
  snprintf(output, max_len, "%s%s", protocol, input);
}

int execute_mount_command(WebDAVConnection* connection, const char* action) {
  char command[MAX_LEN * 2];
//返回相应的错误码或成功码
  if (strcmp(action, "mount") == 0) {
    snprintf(command, sizeof(command), "sudo mount -t %s %s %s", DAVFS_TYPE, connection->webdav_url, connection->mount_point);
  } else if (strcmp(action, "umount") == 0) {
    snprintf(command, sizeof(command), "sudo umount %s", connection->mount_point);
  } else {
    fprintf(stderr, "Error: Invalid action '%s'.\n", action);
    return 1;
  }

  if (system(command) != 0) {
    fprintf(stderr, "Error: Failed to execute command '%s'.\n", command);
    return 1;
  }

  printf("Command '%s' executed successfully.\n", command);
  return 0;
}

void print_usage(const char* program_name) {
  printf("Usage: %s [mount|umount] <mount_point> [webdav_url]\n", program_name);
  printf("  mount_point: The local directory to mount the WebDAV resource.\n");
  printf("  webdav_url: The URL of the WebDAV server (required for mount).\n");
}

int update_fstab(WebDAVConnection* connection) {
  FILE *fp = fopen("/etc/fstab", "a");
  if (fp != NULL) {
    fprintf(fp, "%s\t%s\tdavfs\tdefaults 0 0\n", connection->webdav_url, connection->mount_point);
    fclose(fp);
    return 0;
  } else {
    fprintf(stderr, "Error: Unable to open /etc/fstab for writing.\n");
    return 1;
  }
}
